package com.demo2do.fms.admin.web.controller.system;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.authority.UserService;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.entity.authority.User;
/**
 * 
 * @author Ysy
 *
 */
@Controller
@RequestMapping("/profile")
@MenuSetting("menu-authority")
public class ProfileController {
    
    @Autowired
    private UserService userService;

    /**
     * 个人中心
     *
     * @param login
     * @return
     */
    @RequestMapping(value = "" , method = RequestMethod.GET)
    @MenuSetting("submenu-profile-info")
    public ModelAndView profile(@Secure Login login){
                      
        ModelAndView modelAndView = new ModelAndView("system/profile/profile");
        
        User user = userService.load(User.class, login.getId());
        
        modelAndView.addObject("profile", user);
        modelAndView.addObject("login", login);
        
        return modelAndView;
    }

    /**
     * edit password
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/edit-password", method = RequestMethod.GET)
    @MenuSetting("submenu-profile-info")
    public ModelAndView editPassword(@PathVariable("id") Long id) {

        ModelAndView modelAndView = new ModelAndView("system/profile/profile-edit-password");
        
        modelAndView.addObject("id", id);

        return modelAndView;
    }
    
    /**
     * check exist password whether valid
     * 
     * @param id
     * @param existPassword
     * @return
     */
    @RequestMapping(value = "/{id}/check-password", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult checkExistPassword(@PathVariable("id") Long id, 
                                                       @RequestParam("existPassword") String existPassword) {
        
        HttpResult httpResult = new HttpResult();
        
        User user = userService.load(User.class, id);
        boolean validPassword = userService.checkExistPassword(user, existPassword); 
        
        httpResult.success().data("validPassword", validPassword);
        
        return httpResult;
        
    }

    /**
     * on edit
     *
     * @param id
     * @param password
     * @param operator
     * @return
     */
    @RequestMapping(value = "/{id}/edit-password", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onEditPassword(@PathVariable("id") Long id,
                                                   @RequestParam("password") String password,
                                                   @Secure(property = "id") Long operator) {

        HttpResult httpResult = new HttpResult();
        
        User profile = userService.load(User.class, id);
        userService.editPassword(profile, password, operator);
        
        httpResult.success().message("修改成功");

        return httpResult;
    }
    
    /**
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/upload-example", method = RequestMethod.GET)
    @MenuSetting("submenu-profile-info")
    public ModelAndView uploadExample(@Secure(property = "id") Long id) {
        
        ModelAndView modelAndView = new ModelAndView("system/profile/profile-upload-example");
        
        User user = userService.load(User.class, id);
        
        modelAndView.addObject("profile", user);
        return modelAndView;
    }
    
    /**
     * 
     * @param input
     * @param file
     * @param file2
     * @return
     */
    @RequestMapping(value = "/upload-example", method = RequestMethod.POST)
    public @ResponseBody HttpResult onUploadExample(@RequestParam("example-input") String input,
                                                    @RequestParam(value = "example-file") MultipartFile file,
                                                    @RequestParam(value = "example-file-2") MultipartFile file2) {
        
        HttpResult httpResult = new HttpResult();
        
        return httpResult;
    }
}
